<div class="layuimini-main">
	<div class="layui-form layuimini-form" lay-filter="update-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">模板名称</label>
			<div class="layui-input-block">
				<input type="text" name="templateName" lay-verify="required" lay-reqtext="模板名称不能为空" placeholder="如：测试环境模板" autocomplete="off" class="layui-input">
			</div>
			<input type="hidden" id="id" name="id"/>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">环境名称</label>
			<div class="layui-input-block">
				<input type="text" name="envName" lay-verify="required" lay-reqtext="环境名称不能为空" placeholder="如：测试" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">环境标识</label>
			<div class="layui-input-block">
				<input type="text" name="tag" lay-verify="required" lay-reqtext="环境标识不能为空" placeholder="如：qa" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">部署集群</label>
			<div class="layui-input-block">
				<select name="clusterId" id="clusterId" lay-filter="clusterId" lay-verify="required" lay-reqtext="部署集群不能为空">
					<option value="">请选择或输入</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">部署命名空间</label>
			<div class="layui-input-block">
				<select name="namespaceName" id="namespaceName" lay-filter="namespaceName" lay-verify="required" lay-reqtext="部署命名空间不能为空">
					<option value="">请选择或输入</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">部署序号</label>
			<div class="layui-input-block">
				<input name="deploymentOrder" placeholder="值越小越往前，默认值：0" autocomplete="off"  class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">服务端口</label>
			<div class="layui-input-block">
				<input type="text" name="servicePort" placeholder="如：8080" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">辅助端口</label>
			<div class="layui-input-block">
				<input type="text" name="minorPorts" id="minorPorts" placeholder="辅助服务端口，如：8081,8082" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">最小副本数</label>
			<div class="layui-input-block">
				<input type="text" name="minReplicas" placeholder="默认值：1" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">最大副本数</label>
			<div class="layui-input-block">
				<input type="text" name="maxReplicas" placeholder="默认值：1" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">每副本CPU（m）</label>
			<div class="layui-input-block">
				<input type="text" name="replicaCpu" placeholder="默认值：1000" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">每副本内存（MB）</label>
			<div class="layui-input-block">
				<input type="text" name="replicaMemory" placeholder="默认值：1024" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">自动扩容CPU率（%）</label>
			<div class="layui-input-block">
				<input type="text" name="autoScalingCpu" placeholder="默认值：80" autocomplete="off" class="layui-input">
			</div>
		</div>
		<!--
		<div class="layui-form-item">
			<label class="layui-form-label">自动扩容内存率（%）</label>
			<div class="layui-input-block">
				<input type="text" name="autoScalingMemory" placeholder="默认值：80" autocomplete="off" class="layui-input">
			</div>
		</div>
		-->
		<div class="layui-form-item">
			<label class="layui-form-label">部署审批</label>
			<div class="layui-input-block">
				<input type="radio" name="requiredDeployApproval" value="0" title="否" checked>
				<input type="radio" name="requiredDeployApproval" value="1" title="是">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">部署后合并代码</label>
			<div class="layui-input-block">
				<input type="radio" name="requiredMerge" value="0" title="否" checked>
				<input type="radio" name="requiredMerge" value="1" title="是">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">链路追踪状态</label>
			<div class="layui-input-block">
				<input type="radio" name="traceStatus" lay-filter="traceStatus" value="0" title="否" checked>
				<input type="radio" name="traceStatus" lay-filter="traceStatus" value="1" title="是">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">描述</label>
			<div class="layui-input-block">
				<input type="text" name="description" placeholder="不超过100字" autocomplete="off" class="layui-input">
			</div>
		</div>
		<input type="hidden" name="appEnvId" id="appEnvId">
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
			parentIndex = layer.index;
		var id = JSON.parse(parent.data2).id;
		
		$("#id").val(id);
		
		$.ajax({
			url: '/globalConfig/query',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"globalConfigId": id}),
			success: function(res){
				var data = res.data.envTemplate;
				$("#appEnvId").val(data.id);
				form.val("update-form", {
					"templateName": data.templateName
					,"envName": data.envName
					,"tag": data.tag
					,"deploymentOrder": data.deploymentOrder
					,"minReplicas": data.minReplicas
					,"maxReplicas": data.maxReplicas
					,"replicaCpu": data.replicaCpu
					,"replicaMemory": data.replicaMemory
					,"autoScalingCpu": data.autoScalingCpu
					,"servicePort": data.servicePort
					,"minorPorts": data.minorPorts
					,"requiredDeployApproval": data.requiredDeployApproval
					,"requiredMerge": data.requiredMerge
					//,"autoScalingMemory": data.autoScalingMemory
					,"description": data.description
				});
				//初始化集群列表
				initCluster(form, $, data.clusterId);
				initNamespaceName(form, $, data.clusterId, data.namespaceName);
			},
			error: function(data){
				layer.msg(data.message, {icon: 5, shift: 6});
			}
		});
		
        form.on('submit(saveBtn)', function(data) {
			$.ajax({
				url: '/globalConfig/envTemplate/update',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(data){
					if(data.code != "000000"){
						layer.msg(data.message, {icon: 5, shift: 6});
						return false;
					}
					layer.close(parentIndex);
					layer.msg("保存成功", {
						icon: 1,
						time: 500,
						shade: 0.01,
						shadeClose: false}, function(){
						parent.layui.table.reload('currentTableId');
					});
				},
				error: function(data){
					layer.close(parentIndex);
					layer.msg(data.message, {icon: 5, shift: 6});
				}
			});
            return false;
        });
    });
	
	function initCluster(form, $, selectedClusterId){
		$.ajax({
			url: '/cluster/search',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({'1': 1}),
			success: function(res){
				if(res.code != '000000'){
					return false;
				}
				data = res.data;
				var opt = '';
				for(var k in data){
					if(selectedClusterId == data[k].id){
						opt = opt.concat("<option value='"+ data[k].id +"' selected>"+ data[k].clusterName +"</option>");
					}else{
						opt = opt.concat("<option value='"+ data[k].id +"'>"+ data[k].clusterName +"</option>");
					}
					
				}
				$("#clusterId").append(opt);
				form.render('select');
			},
			error: function(data){
				layer.msg(data.message, {icon: 5, shift: 6});
			}
		});
	}
	
	function initNamespaceName(form, $, clusterId, selectedNamespace){
		$.ajax({
			url: '/cluster/namespace/page',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({'clusterId': clusterId, 'pageSize': 1000, 'pageNum': 1}),
			success: function(res){
				if(res.code != '000000'){
					return false;
				}
				var opt = '';
				data = res.data.items;
				for(var k in data){
					if(data[k].namespaceName == selectedNamespace){
						opt = opt.concat("<option value='"+ data[k].namespaceName +"' selected>"+ data[k].namespaceName +"</option>");
					}else{
						opt = opt.concat("<option value='"+ data[k].namespaceName +"'>"+ data[k].namespaceName +"</option>");
					}
				}
				$("#namespaceName").html(opt);
				form.render('select');
			},
			error: function(data){
				layer.msg(data.message, {icon: 5, shift: 6});
			}
		});
	}
</script>